/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mytdev.commons.util;

import com.mytdev.commons.text.EnumParserFactory;
import com.mytdev.commons.text.ServiceParserFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Yann D'Isanto
 */
public final class StringUtil implements StringConstants {

    private StringUtil() {
    }

    public static String[] extract(String string, Pattern pattern) {
        List<String> arrayList = new ArrayList<String>();
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            arrayList.add(matcher.group());
        }
        return arrayList.toArray(new String[arrayList.size()]);
    }

    public static String[] extract(String string, String regex) {
        return extract(string, Pattern.compile(regex));
    }

    public static <T> String implode(String glue, T... pieces) {
        StringBuilder sb = new StringBuilder();
        for (T piece : pieces) {
            sb.append(piece);
            sb.append(glue);

        }
        if (sb.length() > 0) {
            sb.setLength(sb.length() - glue.length());
        }
        return sb.toString();
    }

    public static String implode(String glue, Iterable<?> pieces) {
        StringBuilder sb = new StringBuilder();
        for (Object piece : pieces) {
            sb.append(piece);
            sb.append(glue);

        }
        if (sb.length() > 0) {
            sb.setLength(sb.length() - glue.length());
        }
        return sb.toString();
    }

    public static String bytesToHexa(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            if (v < 0x10) {
                sb.append(StringConstants.$0);
            }
            sb.append(Integer.toString(v, 16));
        }
        return sb.toString();
    }

    public static String format(Color color) {
        int red = color.getRed();
        int green = color.getGreen();
        int blue = color.getBlue();
        StringBuilder sb = new StringBuilder(StringConstants.HASH);
        if (red < 0x10) {
            sb.append(StringConstants.$0);
        }
        sb.append(Integer.toHexString(red));
        if (green < 0x10) {
            sb.append(StringConstants.$0);
        }
        sb.append(Integer.toHexString(green));
        if (blue < 0x10) {
            sb.append(StringConstants.$0);
        }
        sb.append(Integer.toHexString(blue));
        return sb.toString();
    }

    public static String format(Dimension dimension) {
        return new StringBuilder().append(dimension.width).append(StringConstants.SPACE).append(dimension.height).toString();
    }

    public static <T> T parseObject(String value, Class<T> type) throws ParseException {
        if (Enum.class.isAssignableFrom(type)) {
            return EnumParserFactory.getInstance().getParser(type).parse(value);
        } else {
            return ServiceParserFactory.getInstance().getParser(type).parse(value);
        }
    }

}
